package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class FamilyDao {
    private Context mContext;
    private Dao<Family, Integer> familyDaoOpe;
    private DatabaseHelper helper;

    public FamilyDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            familyDaoOpe = helper.getDao(Family.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(Family family) {
        try {
            family.setChecked(false);
            familyDaoOpe.create(family);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateFamily(Family family) {
        try {
            return familyDaoOpe.update(family);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public Family get(int id) {
        try {
            return familyDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public Family getSelect() {
        try {
            List<Family> families = getAll();
            if (families.size() < 1) return null;
            for (Family family : families) {
                if (family.isChecked()) return family;
            }
            Family family = families.get(0);
            family.setChecked(true);
            updateFamily(family);
            return family;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<Family> getAll() {
        List<Family> familyList = new ArrayList<>();
        try {
            familyList = familyDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return familyList;
    }

    public Family getById(String familyId) {
        List<Family> familyList = null;
        try {
            familyList = familyDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).query();
            if (familyList != null && familyList.size() > 0) {
                return familyList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Family> getFamiliesByType(int type) {
        List<Family> familyList = new ArrayList<>();
        try {
            familyList = familyDaoOpe.queryBuilder().where().eq("type",type).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return familyList;

    }

    public List<Family> getByBlType(int blType) {
        List<Family> familyList = new ArrayList<>();
        try {
            familyList = familyDaoOpe.queryBuilder().where().eq("blType",blType).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return familyList;

    }

    public int delete(Family family) {
        try {
            return familyDaoOpe.delete(family);
            //    LogHelper.i("deleFamilynumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }


    public void deleAll() {
        try {
            int number = familyDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<Family> familyList) {
        try {
            int number = familyDaoOpe.delete(familyList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
